package org.ssssssss.magicapi.socket;

import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;

import org.ssssssss.magicapi.Const;
import org.tio.core.ChannelContext;
import org.tio.core.TioConfig;
import org.tio.core.exception.TioDecodeException;
import org.tio.core.intf.Packet;
import org.tio.server.intf.TioServerHandler;



/**
 * @author zcg
 */
//@Component
public class R2rServerHandler implements TioServerHandler {
	private String decodeApi = "";
	private String encodeApi = "";
	private String handlerApi = "";
   /**
     * 解码：把接收到的ByteBuffer，解码成应用可以识别的业务消息包
     * 总的消息结构：消息头 + 消息体
     * 消息头结构：    4个字节，存储消息体的长度
     * 消息体结构：   对象的json串的byte[]
     */
    @Override
    public Packet decode(ByteBuffer buffer, int limit, int position, int readableLength, ChannelContext channelContext) throws TioDecodeException {
        if(readableLength>0) {
	    	Map<String,Object> params = new HashMap<>();
	        params.put("buffer",buffer);
	        params.put("limit",limit);
	        params.put("position",position);
	        params.put("readableLength",readableLength);
	        params.put("channelContext",channelContext);
	        return Const.getService().execute("POST",decodeApi, params);        	
        }
        return null;
    	

        /*
        //提醒：buffer的开始位置并不一定是0，应用需要从buffer.position()开始读取数据
        //收到的数据组不了业务包，则返回null以告诉框架数据不够
        if (readableLength < R2rPacket.HEADER_LENGHT) {
            return null;
        }
        //读取消息体的长度
        int bodyLength = buffer.getInt();
        //数据不正确，则抛出AioDecodeException异常
        if (bodyLength < 0) {
            throw new TioDecodeException("bodyLength [" + bodyLength + "] is not right, remote:" + channelContext.getClientNode());
        }
        //计算本次需要的数据长度
        int neededLength = R2rPacket.HEADER_LENGHT + bodyLength;
        //收到的数据是否足够组包
        int isDataEnough = readableLength - neededLength;
        // 不够消息体长度(剩下的buffer组不了消息体)
        if (isDataEnough < 0) {
            return null;
        } else //组包成功
        {
            R2rPacket imPacket = new R2rPacket();
            if (bodyLength > 0) {
                byte[] dst = new byte[bodyLength];
                buffer.get(dst);
                imPacket.setBody(dst);
            }
            return imPacket;
        }
        */
    }
    /**
     * 编码：把业务消息包编码为可以发送的ByteBuffer
     * 总的消息结构：消息头 + 消息体
     * 消息头结构：    4个字节，存储消息体的长度
     * 消息体结构：   对象的json串的byte[]
     */
    @Override
    public ByteBuffer encode(Packet packet, TioConfig tioConfig, ChannelContext channelContext) {
        Map<String,Object> params = new HashMap<>();
        params.put("packet",packet);
        params.put("channelContext",channelContext);
        ByteBuffer buffer = Const.getService().execute("POST",encodeApi, params);
        return buffer;
    }
    /**
     * 处理消息
     */
    @Override
    public void handler(Packet packet, ChannelContext channelContext) throws Exception {
        Map<String,Object> params = new HashMap<>();
        params.put("packet",packet);
        params.put("channelContext",channelContext);
        Const.getService().execute("POST",handlerApi, params);
    }

    /*
     * 设置magic-api接口
     */
    public void setApi(String decodeApi,String encodeApi,String handlerApi) {
    	this.decodeApi = decodeApi;
    	this.encodeApi = encodeApi;
    	this.handlerApi = handlerApi;
    }

}